<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb">	                        	
                        		<li>
		                        	<a href="javascript:start()">顶级分类列表</a>
		                        </li>
		                        <li>
		                       		<a href="#" >珠宝</a>
		                        </li>
		                        <li>
		                        	<a href="#" >银饰</a>
		                        </li>
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteById()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody"></tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类增加</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td>
		      		   珠宝 >>  银饰
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input name="name" id="name" class="form-control" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>
		      			<input name="typeId" id="typeId" select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="save()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 修改窗口 -->
  <div class="modal fade" id="editModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel2">商品分类增加</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>上级商品分类</td>
						  <td>
							  珠宝 >>  银饰
						  </td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>
						  <td><input name="name" id="name_update" class="form-control" placeholder="商品分类名称">
						  <input name="id" id="id_update" type="hidden" class="form-control" placeholder="商品分类id">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
							  <input name="typeId" id="typeId_update" select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" onclick="updateInfo()" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
</html>
<script>
	$(function (){
		itemCatList()
	})
	//默认顶级为0
	var parentId = 0;
		function itemCatList(){
		$.ajax({
			url : "/item/itemCatList",
			data : {parentId:parentId},
			async : false,
			dataType : "json",
			type : "post",
			success:function (result){
				console.log(result)
				if(result.code == 10000){
					var data = result.data;
					var tr = '';
					for(var i=0;i<data.length;i++){
						tr+='<tr>';
						tr+='<td><input id="box" value="'+data[i].id+'" type="checkbox"></td>';
						tr+='<td>'+data[i].id+'</td>';
						tr+='<td>'+data[i].name+'</td>';
						tr+='<td>'+data[i].typeId+'</td>';
						tr+='<td class="text-center">';
						tr+='<button type="button" onclick="selectId('+data[i].id+')" class="btn bg-olive btn-xs">查询下级</button>';
						tr+='<button type="button" class="btn bg-olive btn-xs" onclick="updateById('+data[i].id+')" data-toggle="modal" data-target="#editModal2">修改</button>';
						tr+='</td>';
						tr+='</tr>';
					}
					$("#tbody").html(tr)

				 	if(result.data.length == 0){
						var tr = '';
						tr += '<tr><th colspan="5">已经最后一条咯!!!</th></tr>'
						$("#tbody").html(tr)
					}
				}

			},
			error:function (data){
				alert("网络异常")
			}
		})
	}

	/*将当前页面id作为下一个父级id*/
	function selectId(id){
		parentId = id;
		itemCatList();
	}
	/*初始化父级ID*/
	function start(){
		parentId = 0;
		itemCatList();
	}
	/*增加*/
	function save(){
		$.ajax({
			url : "/item/save",
			data : {name:$("#name").val(),typeId:$("#typeId").val(),parentId:parentId},
			async : false,
			dataType : "json",
			type : "post",
			success:function (result){
				if (result.code == 10000) {
					window.location.reload();
					alert(result.message);
				} else {
					alert(result.message);
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}
	/*回显*/
	function updateById(id){
		$.ajax({
			url : "/item/updateById",
			data : {id:id},
			async : false,
			dataType : "json",
			type : "post",
			success:function (result){
				console.log(result)
				if(result.code == 10000){
					var data = result.data;
					$("#id_update").val(data.id)
					$("#name_update").val(data.name)
					$("#typeId_update").val(data.typeId)
				}

			},
			error:function (data){
				alert("网络异常")
			}
		})
	}
	/*修改*/
	function updateInfo(){
		$.ajax({
			url : "/item/updateInfo",
			data : {id:$("#id_update").val(),name:$("#name_update").val(),typeId:$("#typeId_update").val(),parentId:parentId},
			async : false,
			dataType : "json",
			type : "post",
			success:function (result){
				if (result.code == 10000) {
					window.location.reload();
					alert(result.message);
				} else {
					alert(result.message);
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}
	/*批量删除*/
	function deleteById(){
		var ids = $("#box:checked")
		var carIds = [];
		for (var i = 0; i < ids.length; i++) {
			carIds.push(ids[i].value)
		}
		$.ajax({
			url: "/item/deleteCarById",
			data: {id: carIds},
			dataType: "json",
			type: "post",
			async: true,
			success: function (result) {
				window.location.reload();
				alert(result.message);

			},
			error: function (result) {
				alert("系统异常")
			}
		})
	}
</script>